
#include <Python.h>
#include <numpy/arrayobject.h>
#include <cassert>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Video{
	public:
		Video(long x, long y);
		Video(long x, long y, char* path);
		~Video();
		void refresh();
		int* matriz();
		void pintar(char* s);
		void waitForClick(int*);
		void getRect(int*);
	private:
		PyObject* pVideo;
};

Video::Video(long x, long y, char* path){
	
	cout << "estoy anes del python loco" << endl;
	//variables
	PyObject *pNombre, *pDict, *pX, *pY, *pTup, *pArgs;
	//otros
	PyObject *pModulo, *cVideo;
	Py_Initialize();
	
	cout << "estoy en python loco" << endl;
    // Importar modulos standard

	string instruction("sys.path.append(os.path.dirname(os.path.abspath(");
	instruction += path;
	instruction +=")))";

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("import os");
	PyRun_SimpleString("import time");
	PyRun_SimpleString(instruction.data());

    // Importar el modulo genVideo
	pNombre = PyString_FromString("genVideo");
    pModulo = PyImport_Import(pNombre);

    // Obetener dicciconario de namespace de este modulo
    pDict = PyModule_GetDict(pModulo);

	// obtener la clase Video de este modulo
	cVideo = PyDict_GetItemString(pDict, "Video");

	// armar los argumentos que le vamos a pasar al constructor d esta clase
	pX 		= PyInt_FromLong(x);
	pY 		= PyInt_FromLong(y);
	pTup 	= PyTuple_Pack(2, pX, pY);
	pArgs 	= PyTuple_Pack(1, pTup);
	
	//pVideo es la clase que maneja el video
	this->pVideo 	= PyObject_CallObject(cVideo,pArgs);
	return;
}

Video::Video(long x, long y){

	//variables
	PyObject *pNombre, *pDict, *pX, *pY, *pTup, *pArgs;
	//otros
	PyObject *pModulo, *cVideo;
	Py_Initialize();

    // Importar modulos standard

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("import os");
	PyRun_SimpleString("import time");
	PyRun_SimpleString("sys.path.append(os.getcwd())");

    // Importar el modulo genVideo
	pNombre = PyString_FromString("genVideo");
    pModulo = PyImport_Import(pNombre);

    // Obetener dicciconario de namespace de este modulo
    pDict = PyModule_GetDict(pModulo);

	// obtener la clase Video de este modulo
	cVideo = PyDict_GetItemString(pDict, "Video");

	// armar los argumentos que le vamos a pasar al constructor d esta clase
	pX 		= PyInt_FromLong(x);
	pY 		= PyInt_FromLong(y);
	pTup 	= PyTuple_Pack(2, pX, pY);
	pArgs 	= PyTuple_Pack(1, pTup);
	
	//pVideo es la clase que maneja el video
	this->pVideo 	= PyObject_CallObject(cVideo,pArgs);
	return;
}

void Video::pintar(char* color){
	PyObject_CallMethod(pVideo, "pintar", "s",color,NULL);
	return;
}
Video::~Video(){
	Py_DECREF(this->pVideo);
	Py_Finalize();
}

void Video::refresh(){
	PyObject_CallMethod(this->pVideo, "refresh", NULL);
}

int* Video::matriz(){
	return (int*)PyArray_DATA(PyObject_GetAttrString(this->pVideo,"array"));
}

void Video::waitForClick(int* pos){
	PyObject* pTup;
	pTup =	PyObject_CallMethod(this->pVideo, "waitForClick",NULL);
	pos[0] = (int)PyInt_AsLong(PyTuple_GetItem(pTup, (Py_ssize_t)0));
	pos[1] = (int)PyInt_AsLong(PyTuple_GetItem(pTup, (Py_ssize_t)1));
	return;
}

void Video::getRect(int* pos){
	PyObject* pTup, *pIni, *pFini;
	pTup =	PyObject_CallMethod(this->pVideo, "waitForClick",NULL);
	pIni = PyTuple_GetItem(pTup, (Py_ssize_t)0);
	pFini = PyTuple_GetItem(pTup, (Py_ssize_t)1);
	pos[0] = (int)PyInt_AsLong(PyTuple_GetItem(pIni, (Py_ssize_t)0));
	pos[1] = (int)PyInt_AsLong(PyTuple_GetItem(pIni, (Py_ssize_t)1));
	pos[2] = (int)PyInt_AsLong(PyTuple_GetItem(pFini, (Py_ssize_t)0));
	pos[3] = (int)PyInt_AsLong(PyTuple_GetItem(pFini, (Py_ssize_t)1));
	return;
}



